package moplayer;

public class PlayerRT extends Thread {

	private Boolean TRIGGER_CROMATIC_DECRESCENTE;
	private Boolean TRIGGER_CROMATIC_CRESCENTE;
	private Boolean TRIGGER_ARPEGGIO_DECRESCENTE;
	private Boolean TRIGGER_ARPEGGIO_CRESCENTE;
	private Boolean TRIGGER_NOTES_FLOW;
	private float NOTE_DYNAMICS;

	private float lastY;

	TrackRT track;
	MyPlayerModel player;
	NoteRT lastNote;
	boolean locked;
	int channel = 5;
	boolean debug = false;

	public PlayerRT(TrackRT track, MyPlayerModel player) {
		this.track = track;
		this.player = player;
	}

	@Override
	public void run() {
		while (!isInterrupted()) {
			NoteRT note = null;
			updateValues();
			if (TRIGGER_NOTES_FLOW || debug) {
				note = track.getNoteToPlay();
				executeDefaultOp(note);
			}

			if (TRIGGER_ARPEGGIO_CRESCENTE) {
				note = track.getArpeggioNote(true);
				executeDefaultOp(note);
			}

			if (TRIGGER_ARPEGGIO_DECRESCENTE) {
				note = track.getArpeggioNote(false);
				executeDefaultOp(note);
			}

			if (TRIGGER_CROMATIC_CRESCENTE) {
				note = track.getCromaticNote(true);
				executeDefaultOp(note);
			}

			if (TRIGGER_CROMATIC_DECRESCENTE) {
				note = track.getCromaticNote(false);
				executeDefaultOp(note);
			}

			if (DeviceValueOSCFiller.command.equals("/buttonAPressed") && !locked) {
				// if (DeviceValueOSCFiller.pressed("/buttonA") && !locked) {
				note = track.getNoteToPlay();
				lastNote = note;
				this.player.noteOn(note, channel);
				locked = true;
			} else if (DeviceValueOSCFiller.command.equals("/buttonAReleased")) {
				this.player.noteOff(lastNote, channel);
				locked = false;
			}

			if (DeviceValueOSCFiller.command.equals("/buttonPlusPressed") && !locked) {
				float bpm = this.player.getBpm();
				System.out.println("BPM = " + bpm);
				this.player.setBpm(bpm + 10);
				locked = true;
			} else if (DeviceValueOSCFiller.command.equals("/buttonPlusReleased")) {
				locked = false;
			}

			if (DeviceValueOSCFiller.command.equals("/buttonMinusPressed") && !locked) {
				float bpm = this.player.getBpm();
				System.out.println("BPM = " + bpm);
				this.player.setBpm(bpm - 10);
				locked = true;
			} else if (DeviceValueOSCFiller.command.equals("/buttonMinusReleased")) {
				locked = false;
			}

			if (DeviceValueOSCFiller.command.equals("/buttonHomePressed")) {
				this.player.setBpm(120f);
			}

			// if(DeviceValueOSCFiller.toggleMap.get("/buttonMinus")){
			// this.player.setBpm(this.player.getBpm() - 20);
			// }

			// float nunchukMap =
			// Utils.map(DeviceValueOSCFiller.nunchukJoystickX, 0, 1, 30, 360);
			// System.out.println("Nunchuk Stick Value = " + nunchukMap);
			// if(!debug){
			// this.player.setBpm(nunchukMap);
			// }
			float difference = Math.abs(DeviceValueOSCFiller.wiiY - lastY);
			if (difference > 0.02) {
				System.out.println("Mudou = " + difference);
			}
			lastY = DeviceValueOSCFiller.wiiY;
		}
	}

	private void executeDefaultOp(NoteRT note) {
		// if(note != null){
		note.setDynamics(Math.round(NOTE_DYNAMICS * 127));
		// note.setDuration(Math.round(DeviceValueOSCFiller.wiiY*500));
		this.player.playNote(note, channel);
		// }
	}

	public void setChannel(int i) {
		this.channel = i;
	}

	private void updateValues() {
		this.TRIGGER_CROMATIC_DECRESCENTE = DeviceValueOSCFiller.buttonMap.get("/buttonDown");
		this.TRIGGER_CROMATIC_CRESCENTE = DeviceValueOSCFiller.buttonMap.get("/buttonUp");
		this.TRIGGER_ARPEGGIO_DECRESCENTE = DeviceValueOSCFiller.buttonMap.get("/buttonRight");
		this.TRIGGER_ARPEGGIO_CRESCENTE = DeviceValueOSCFiller.buttonMap.get("/buttonLeft");
		this.TRIGGER_NOTES_FLOW = DeviceValueOSCFiller.buttonMap.get("/buttonB");
		this.NOTE_DYNAMICS = DeviceValueOSCFiller.wiiX;
	}

}
